"""
MCP插件兼容性检查器
用于检查MCP插件的兼容性
"""

import time
from typing import Dict, Any, List, Optional


class MCPPluginCompatibilityChecker:
    """MCP插件兼容性检查器"""
    
    REQUIRED_INTERFACES = [
        'register',
        'unregister', 
        'execute',
        'get_tools'
    ]
    
    def __init__(self):
        self.compatibility_reports = {}
    
    def check_plugin_compatibility(self, plugin: Any, plugin_name: str) -> Dict[str, Any]:
        """检查插件兼容性"""
        report = {
            'plugin_name': plugin_name,
            'timestamp': time.time(),
            'compatible': True,
            'issues': [],
            'warnings': []
        }
        
        # 检查必需接口
        for interface in self.REQUIRED_INTERFACES:
            if not hasattr(plugin, interface):
                report['compatible'] = False
                report['issues'].append(f"缺少必需接口: {interface}")
        
        # 检查配置属性
        if not hasattr(plugin, 'config'):
            report['warnings'].append("插件没有配置属性")
        
        # 检查版本兼容性
        if hasattr(plugin, '__version__'):
            version = getattr(plugin, '__version__')
            # 可以添加更复杂的版本兼容性检查逻辑
        else:
            report['warnings'].append("插件没有版本信息")
        
        self.compatibility_reports[plugin_name] = report
        return report
    
    def generate_compatibility_report(self) -> Dict[str, Any]:
        """生成兼容性报告"""
        total_plugins = len(self.compatibility_reports)
        compatible_plugins = sum(
            1 for report in self.compatibility_reports.values() 
            if report['compatible']
        )
        
        return {
            'total_plugins': total_plugins,
            'compatible_plugins': compatible_plugins,
            'incompatible_plugins': total_plugins - compatible_plugins,
            'compatibility_rate': compatible_plugins / total_plugins if total_plugins > 0 else 0,
            'reports': self.compatibility_reports
        }